#ifndef RESCACHE_H
#define RESCACHE_H

/* Copyright (C) 2011 Riccardo Marson

   This file is part of OpenPool, an open-source game of pool.

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included
   in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   RICCARDO MARSON BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
   AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  See Readme.rtf for further information.

*/

//glew must be included before any other openGL related includes.
#include "Glew/glew.h"

#include "SFML/Config.hpp"

#ifdef SFML_SYSTEM_WINDOWS
    #include <windows.h>
#endif

#include "PhysFS/physfs.h"
#include "FreeImage/FreeImage.h"
#include "model_obj.h"

#include "SFML/Audio.hpp"

#include "interfaces.h"
#include "logmanager.h"

#include <list>
#include <map>

/*
  Resource types and relative handles
*/

// ****** Sounds and sound buffers

class CAudioResHandle : public IResHandle
{
public:

    CAudioResHandle() {}
	virtual ~CAudioResHandle() {}

    eResourceType getType();

    sf::SoundBuffer soundBuffer;
    sf::Sound sound;
};

class CAudioResource : public IResource
{
public:

    CAudioResource() {}
	virtual ~CAudioResource() {}

    eResourceType getType();
	ResHandlePtr getHandle();
	bool load(string resName);
	bool unload();

private:

    ResHandlePtr mHandle;
};


// ****** Textures

class CImageResHandle : public IResHandle
{
public:

    CImageResHandle() {}
	virtual ~CImageResHandle() {}

    eResourceType getType();

    GLuint glTextureID;
};

class CImageResource : public IResource
{
public:

    CImageResource() {}
	virtual ~CImageResource() {}

    eResourceType getType();
	ResHandlePtr getHandle();
	bool load(string resName);
	bool unload();

private:

	GLuint loadTexture(string fileName, PHYSFS_sint64 size, BYTE* buffer);
    ResHandlePtr mHandle;
};

// ****** 3D models

class CObjResHandle : public IResHandle
{
	friend class CObjResource;

public:

    CObjResHandle() {}
	virtual ~CObjResHandle() {}

    eResourceType getType();

    const ModelOBJ* model();

private:

    ModelOBJ mModel;
};

class CObjResource : public IResource
{
public:

    CObjResource() {}
	virtual ~CObjResource() {}

    eResourceType getType();
	ResHandlePtr getHandle();
	bool load(string resName);
	bool unload();

private:

    ResHandlePtr mHandle;
};

/*
  Resource cache based on PhysFS
*/
class CResourceCache
{
public:

    CResourceCache(const char *argv0);
    ~CResourceCache();

    static CResourceCache* getSingletonPtr();

    // resName: name of the resource file without path.
    bool registerSearchPath(string searchPath);
    ResHandlePtr requestResource(string resName, eResourceType resType);
    char* requestScriptSource(string srcName);
    bool unloadResource(string resName);
    void unloadAllResources();

private:

    // private types
    typedef boost::shared_ptr<IResource> ResPtr;
    typedef map<string, ResPtr> ResMap;

    // members
    static CResourceCache* uniqueInstance;

    string mRunDirectory; // full path to the directory from which the executable is being run.
    list<string> mSearchPaths; // search directories and / or archives with relative paths

    ResMap mResources;

};

#endif // RESCACHE_H
